<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\AdminUserModel;
use App\Models\yb\NetworkPointsModel;
use Illuminate\Http\Request;

class MerchantController extends Controller
{

    /**
     * Get a JWT via given credentials.
     *  商户列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function MerchantListing(Request $request){

        $page = $request->post('page', 1);

        $take = $request->post('take', 100);

        $title = $request->post('title');

        if(!empty($title)){
            $data = AdminUserModel::where('username','!=','admin')->where('name','like','%'.$title.'%')->select([
                'id','mobile','name','distribution_scope','address'
            ])->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();
        }else{
            $data = AdminUserModel::where('username','!=','admin')->select([
                'id','mobile','name','distribution_scope','address'
            ])->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();
        }

        foreach($data as $key=>$val){
            $data[$key]['avatar'] = env('OSS_URL').$val['avatar'];
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }
    
     /**
     * Get a JWT via given credentials.
     *  区域划分提交
     * @return \Illuminate\Http\JsonResponse
     */
    public function admin_points(Request $request){

        $points = $request->post('points');
        if(empty($points)){
            return response()->json(['code'=>'201','msg' => '请框选网点区域']);
        }
        
        $points_data = $request->post('points_data');
        if(empty($points_data)){
            return response()->json(['code'=>'201','msg' => '请框选网点区域']);
        }

        $admin_id = $request->post('admin_id');
        if(empty($admin_id)){
            return response()->json(['code'=>'201','msg' => '请选择网点']);
        }
        
        $points_id = $request->post('points_id');
        
        if(!empty($points_id)){
            NetworkPointsModel::where(['id'=>$points_id])->update([
                'admin_id'=>$admin_id,
                'points'=>$points,
                'points_data'=>$points_data,
            ]);
        }else{
             NetworkPointsModel::insert([
                'admin_id'=>$admin_id,
                'points'=>$points,
                'points_data'=>$points_data,
                'created_at'=> date('Y-m-d H:i:s',time())
            ]);
        }

       
        

        return response()->json(['code'=>'200','msg' => '设置成功']);

    }
    
    /**
     * Get a JWT via given credentials.
     *  区域划分删除
     * @return \Illuminate\Http\JsonResponse
     */
    public function admin_points_delete(Request $request){

        $points_id = $request->post('points_id');
        if(empty($points_id)){
            return response()->json(['code'=>'201','msg' => '请选择区域']);
        }

        NetworkPointsModel::where(['id'=>$points_id])->delete();

        return response()->json(['code'=>'200','msg' => '设置成功']);

    }
    
       /**
     * Get a JWT via given credentials.
     *  区域划分列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function admin_points_listing(Request $request){

        $list = array();
        $data = NetworkPointsModel::select(['id','admin_id','points','points_data','created_at'])->get();
        foreach($data as $key=>$val){
            $admin_data = AdminUserModel::where(['id'=>$val['admin_id']])->select(['id','name','latitude','longitude'])->first();
            // $points = json_decode($val['points'],true);
            // $areapoints = "";
            // foreach ($points as $k=>$v){
            //     $areapoints.=",".$v['latLng']['lat']."_".$v['latLng']['lng'];
            // }
            // if(!empty($areapoints)){
            //     $areapoints = mb_substr($areapoints,1);
            // }
           
            $list[$key]['id'] = $admin_data['id'];
            $list[$key]['points_id'] = $val['id'];
            $list[$key]['remark'] = $admin_data['name'];
            $list[$key]['name'] = $admin_data['name'];
            $list[$key]['centerpoint'] = $admin_data['longitude'].",".$admin_data['latitude'];
            $list[$key]['operdate'] = strtotime($val['created_at']);
            $list[$key]['points'] = $val['points'];
            $list[$key]['points_data'] = $val['points_data'];
        
           
        }
        return response()->json(['code'=>'200','msg' => '设置成功','data'=>$list]);

    }
}
